package com.yeyks.controller.finance;

import com.yeyks.annotation.NeedToken;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.HotelStatementDownloadParam;
import com.yeyks.common.param.HotelStatementParam;
import com.yeyks.common.param.WithdrawalHotelOrderParam;
import com.yeyks.common.param.finance.WithdrawalPeriodOrderParam;
import com.yeyks.common.result.HotelStatementResult;
import com.yeyks.common.result.OrderListResult;
import com.yeyks.common.utils.excel.ExcelUtil;
import com.yeyks.commonReference.service.order.OrderHeadService;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 总公司对账单接口
 *
 * @author shuai
 * @date 2019-07-09
 */
@Slf4j
@Api(tags = "总公司对账单接口")
@RestController
@RequestMapping("/admin/statement")
public class AdminStatementController {
    @Autowired
    private OrderHeadService orderHeadService;

    @NeedToken
    @ApiOperation(value = "总公司C2B财务对账单列表", notes = "总公司C2B财务对账单列表", response = HotelStatementResult.class)
    @PostMapping("/stat")
    public AjaxResponseBody<PageResult<HotelStatementResult>> stat(HotelStatementParam param) {
        if (AccountType.Administrator != AccountUtils.getLoginUser().getType()) {
            throw new ServiceException("你无权操作");
        }
        PageResult<HotelStatementResult> pageResult = orderHeadService.listStatement(param);
        return AjaxResponseBody.success(pageResult);
    }

    @NeedToken
    @ApiOperation(value = "总公司C2B财务对账单列表下载", notes = "总公司C2B财务对账单列表下载", response = HotelStatementResult.class)
    @PostMapping("/stat/download")
    public void statDownload(HotelStatementDownloadParam param, HttpServletResponse response) {
        if (AccountType.Administrator != AccountUtils.getLoginUser().getType()) {
            throw new ServiceException("你无权操作");
        }
        List<HotelStatementResult> hotelStatementResults = orderHeadService.statementDownload(param);
        ExcelUtil.disposeWriteExcelWithSheets(response, hotelStatementResults, new HotelStatementResult());
    }

    @ApiOperation(value = "C2B查询酒店上次提现到现在的已完成订单", notes = "C2B订单", response = OrderListResult.class)
    @PostMapping("/c2b/list")
    public AjaxResponseBody<PageResult<OrderListResult>> c2bList(@Validated WithdrawalHotelOrderParam param) {
        param.setVipFlag(0);
        PageResult<OrderListResult> pageResult = orderHeadService.listHotel(param);
        return AjaxResponseBody.success(pageResult);
    }

    @NeedToken
    @ApiOperation(value = "v4.1 --> 查询账户上次提现到现在的已完成订单", notes = "vip订单", response = OrderListResult.class)
    @PostMapping("/mini/period/list")
    public AjaxResponseBody<PageResult<OrderListResult>> periodList(@Validated WithdrawalPeriodOrderParam param) {
        if (AccountType.Administrator != AccountUtils.getLoginUser().getType()) {
            throw new ServiceException("你无权访问");
        }
        PageResult<OrderListResult> pageResult = orderHeadService.periodListHotel(param);
        return AjaxResponseBody.success(pageResult);
    }

}
